﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Snake
{
    public partial class Form1 : Form
    {
        Game sGame;
        int oldLevel=1;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (timer1.Enabled == false)
                timer1.Enabled = true;
            if (e.KeyCode == Keys.Up)
            {
                sGame.snakeDirection = direction.UP;
            }
            else if (e.KeyCode == Keys.Down)
            {
                sGame.snakeDirection = direction.DOWN;
            }
            else if (e.KeyCode == Keys.Right)
            {
                sGame.snakeDirection = direction.RIGHT;
            }
            else if (e.KeyCode == Keys.Left)
            {
                sGame.snakeDirection= direction.LEFT;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            sGame = new Game();
            sGame.newSnake();
            refreshImage();
        }
        void refreshImage()
        {
            sGame.refreshBitmap();
            this.pictureBox1.Image = sGame.gameImage;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            sGame.stepSnake();
            if (sGame.level > oldLevel)
            {
                oldLevel = sGame.level;
                timer1.Interval -= 10;
            }
            if (sGame.level == 1)
            {
                timer1.Interval = 200;
                oldLevel = 1;
            }
            refreshImage();
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }
    }
}
